package com.dm0858.bianmin.mine.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dm0858.bianmin.api.ApiService;
import com.dm0858.bianmin.app.MyApp;
import com.dm0858.bianmin.mine.bean.BindPhoneBeanData;
import com.dm0858.bianmin.mine.bean.MyFavoriteBeanData;
import com.dm0858.bianmin.mine.bean.MyReleaseBeanData;
import com.dm0858.bianmin.mine.bean.PostManagerBeanData;
import com.dm0858.bianmin.mine.bean.UserInfoBeanData;
import com.dm0858.bianmin.mine.listener.IResultListener;
import com.dm0858.bianmin.mine.listener.IWXLoginListener;
import com.dm0858.bianmin.mine.listener.IWXWithPhoneListener;
import com.dm0858.bianmin.mine.network.Retrofit;
import com.dm0858.bianmin.mine.network.RetrofitListener;
import com.dm0858.bianmin.mine.tools.LogUtils;
import com.dm0858.bianmin.mine.tools.SPUtils;
import com.dm0858.bianmin.mine.tools.ToastUtils;
import java.util.List;

/**
 * @author 迪迪丶Sir
 * @description 我的页面数据请求
 * @e-mail asd969709719@qq.com
 * @since 2020-05-17
 **/
public class MineModel {

    /**
     * @description 获取验证码
     * @author 迪迪丶Sir
     * @e-mail asd969709719@qq.com
     * @since 2020-05-17
     **/
    public static void sendPhoneCode(Retrofit retrofit, String phone,IResultListener listener) {
       retrofit
               .url(ApiService.SEND_PHONE_CODE)
               .addParam("phone",phone)
               .post(new RetrofitListener() {
                   @Override
                   public void onSuccess(Object object) {
                       ToastUtils.showShort("获取验证码成功");
                       listener.onSuccess(object);
                   }

                   @Override
                   public void onError(int messageCode, String e) {
                       LogUtils.e(e);
                       ToastUtils.showShort("获取验证码失败");
                   }
               });
    }

    /**
     * @description 手机号码登录/注册
     * @author 迪迪丶Sir
     * @e-mail asd969709719@qq.com
     * @since 2020-05-17
     **/
    public static void login(Retrofit retrofit, String phone, String code,RetrofitListener listener) {

        retrofit
                .url(ApiService.PHONE_LOGIN)
                .addParam("phone", phone)
                .addParam("code", code)
                .post(listener);
    }

    /**
     *
     * @description 微信登录
     * @author 迪迪丶Sir
     * @e-mail asd969709719@qq.com
     * @since 2020-05-17
     **/
    public static void wxLogin(Retrofit retrofit, String openid, String nickname, String unionid, String headimgurl, IWXLoginListener listener) {
        retrofit
                .url(ApiService.WX_LOGIN)
                .addParam("openid",openid)
                .addParam("nickname",nickname)
                .addParam("unionid",unionid)
                .addParam("headimgurl",headimgurl)
                .post(new RetrofitListener() {
                    @Override
                    public void onSuccess(Object object) {
                        UserInfoBeanData beanData = JSONObject.parseObject(object.toString(),UserInfoBeanData.class);
                        if (beanData != null){
                            MyApp.userid = beanData.getId();
                            MyApp.phone = beanData.getPhone();
                            MyApp.openid = beanData.getOpenid();
                            MyApp.launchMode = 1;
                            SPUtils.getInstance("user_info").put("userid",MyApp.userid,true);
                            SPUtils.getInstance("user_info").put("phone",MyApp.phone,true);
                            SPUtils.getInstance("user_info").put("avatar",beanData.getHeadimgurl(),true);
                            SPUtils.getInstance("user_info").put("nickName",beanData.getNickname());
                            SPUtils.getInstance("user_info").put("launchMode",MyApp.launchMode,true);
                            listener.bind();
                        }

                    }

                    @Override
                    public void onError(int messageCode, String e) {
                        ToastUtils.showShort("您需要绑定手机号");
                        listener.unBind();
                    }
                });
    }

    /**
     *
     * @description 微信绑定手机号
     * @author 迪迪丶Sir
     * @e-mail asd969709719@qq.com
     * @since 2020-05-17
     **/
    public static void wxWithPhone(Retrofit retrofit, String openid, String phone, String code, IWXWithPhoneListener listener) {
        retrofit
                .url(ApiService.WX_WITH_PHONE)
                .addParam("openid",openid)
                .addParam("phone",phone)
                .addParam("code",code)
                .post(new RetrofitListener() {
                    @Override
                    public void onSuccess(Object object) {
                        BindPhoneBeanData beanData = JSON.parseObject(object.toString(),BindPhoneBeanData.class);
                        if (beanData != null) {
                            MyApp.userid = beanData.getId();
                            MyApp.phone = beanData.getPhone();
                            SPUtils.getInstance("user_info").put("userid",MyApp.userid,true);
                            SPUtils.getInstance("user_info").put("phone",MyApp.phone,true);
                            listener.success();
                        }

                    }

                    @Override
                    public void onError(int messageCode, String e) {
                        ToastUtils.showShort(e);
                    }
                });
    }

    /**
     *
     * @description 用户信息
     * @author 迪迪丶Sir
     * @e-mail asd969709719@qq.com
     * @since 2020-05-17
     **/
    public static void userInfo(Retrofit retrofit,String userid,RetrofitListener listener) {
        retrofit
                .url(ApiService.USER_INFO)
                .addParam("userid",userid)
                .post(listener);
    }

    /**
     *
     * @description 检查是否绑定了手机
     * @author 迪迪丶Sir
     * @e-mail asd969709719@qq.com
     * @since 2020-05-18
     **/
    public static void checkPhone(Retrofit retrofit, String userid, String phone, IBindPhoneListener listener) {
        retrofit
                .url(ApiService.USER_WITH_PHONE)
                .addParam("phone",phone)
                .addParam("userid",userid)
                .post(new RetrofitListener() {
                    @Override
                    public void onSuccess(Object object) {
                        listener.bind();
                    }

                    @Override
                    public void onError(int messageCode, String e) {
                        listener.unBind();
                    }
                });
    }

    /**
     *
     * @description 检查是否绑定了手机
     * @author 迪迪丶Sir
     * @e-mail asd969709719@qq.com
     * @since 2020-05-18
     **/
    public static void checkPhone(Retrofit retrofit, String openid,IBindPhoneListener listener) {
        retrofit
                .url(ApiService.USER_WITH_PHONE)
                .addParam("openid",openid)
                .post(new RetrofitListener() {
                    @Override
                    public void onSuccess(Object object) {
                        listener.bind();
                    }

                    @Override
                    public void onError(int messageCode, String e) {
                        listener.unBind();
                    }
                });
    }

    public interface IBindPhoneListener{

        void bind();

        void unBind();
    }

    /**
     *
     * @description 我的发布
     * @author 迪迪丶Sir
     * @e-mail asd969709719@qq.com
     * @since 2020-05-18
     **/
    public static void userArticle(Retrofit retrofit, String userid, String lat,String lng,int page,IResultListener listener) {
        retrofit
                .url(ApiService.USER_ARTICLE)
                .addParam("userid",userid)
                .addParam("lat",lat)
                .addParam("lng",lng)
                .addParam("page",page)
                .post(new RetrofitListener() {
                    @Override
                    public void onSuccess(Object object) {
                        String data = "{" + "\"data\"" + ":" + object.toString() + "}";
                        List<MyReleaseBeanData.DataBean> list = JSONObject.parseObject(data,MyReleaseBeanData.class).getData();
                        if (list != null && list.size() > 0)
                            listener.onSuccess(list);
                    }

                    @Override
                    public void onError(int messageCode, String e) {
                        if (e.equals("none"))
                            ToastUtils.showShort("暂无数据");
                        else
                            ToastUtils.showShort(e);
                    }
                });
    }

    /**
     *
     * @description 帖子管理
     * @author 迪迪丶Sir
     * @e-mail asd969709719@qq.com
     * @since 2020-05-18
     **/
    public static void postManager(Retrofit retrofit, String userid, String lat,String lng,int page,IResultListener listener) {
        retrofit
                .url(ApiService.USER_ARTICLE)
                .addParam("userid",userid)
                .addParam("lat",lat)
                .addParam("lng",lng)
                .addParam("page",page)
                .post(new RetrofitListener() {
                    @Override
                    public void onSuccess(Object object) {
                        String data = "{" + "\"data\"" + ":" + object.toString() + "}";
                        List<PostManagerBeanData.DataBean> list = JSONObject.parseObject(data,PostManagerBeanData.class).getData();
                        if (list != null && list.size() > 0)
                            listener.onSuccess(list);
                    }

                    @Override
                    public void onError(int messageCode, String e) {
                        if (e.equals("none"))
                            ToastUtils.showShort("暂无数据");
                        else
                            ToastUtils.showShort(e);
                    }
                });
    }

    /**
     *
     * @description 获取帖子详情
     * @author 迪迪丶Sir
     * @e-mail asd969709719@qq.com
     * @since 2020-05-19
     **/
    public static void editPost(Retrofit retrofit,String id,RetrofitListener listener) {
        retrofit
                .url(ApiService.DETAILS_ARTICLE)
                .addParam("id",id)
                .post(listener);
    }

    /**
     *
     * @description 收藏
     * @author 迪迪丶Sir
     * @e-mail asd969709719@qq.com
     * @since 2020-05-20
     **/
    public static void collectArticleAction(Retrofit retrofit,String userid,String id,int collectFlag,IResultListener listener) {
        retrofit
                .url(ApiService.COLLECT_ARTICLE_ACTION)
                .addParam("userid",userid)
                .addParam("id",id)
                .addParam("collectFlag",collectFlag)
                .post(new RetrofitListener() {
                    @Override
                    public void onSuccess(Object object) {
                        listener.onSuccess(object);
                    }

                    @Override
                    public void onError(int messageCode, String e) {
                            ToastUtils.showShort(e);
                    }
                });
    }


    /**
     *
     * @description 我的收藏
     * @author 迪迪丶Sir
     * @e-mail asd969709719@qq.com
     * @since 2020-05-20
     **/
    public static void userCollectArticle(Retrofit retrofit,String userid,IResultListener listener) {
        retrofit
                .url(ApiService.USER_COLLECT_ARTICLE)
                .addParam("userid",userid)
                .addParam("lat","")
                .addParam("lng","")
                .addParam("page",1)
                .post(new RetrofitListener() {
                    @Override
                    public void onSuccess(Object object) {
                        String data = "{" + "\"data\"" + ":" + object.toString() + "}";
                        List<MyFavoriteBeanData.DataBean> list = JSON.parseObject(data,MyFavoriteBeanData.class).getData();
                        listener.onSuccess(list);
                    }

                    @Override
                    public void onError(int messageCode, String e) {
                        if (e.equals("none"))
                            ToastUtils.showShort("暂无数据");
                        else
                            ToastUtils.showShort(e);
                    }
                });
    }

    /**
     *
     * @description 平台
     * @author 迪迪丶Sir
     * @e-mail asd969709719@qq.com
     * @since 2020-05-21
     **/
    public static void sysUpdateTxt(Retrofit retrofit,IResultListener listener) {
        retrofit
                .url(ApiService.SYS_UPDATE_TXT)
                .addParam("type",1)
                .post(new RetrofitListener() {
                    @Override
                    public void onSuccess(Object object) {
                        listener.onSuccess(object);
                    }

                    @Override
                    public void onError(int messageCode, String e) {

                    }
                });
    }

    /**
     *
     * @description 代理
     * @author 迪迪丶Sir
     * @e-mail asd969709719@qq.com
     * @since 2020-05-21
     **/
    public static void applyAgent(Retrofit retrofit,String contactname,String phone,String cardid,String city,IResultListener listener) {
        retrofit
                .url(ApiService.APPLY_AGENT)
                .addParam("contactname",contactname)
                .addParam("phone",phone)
                .addParam("cardid",cardid)
                .addParam("city",city)
                .post(new RetrofitListener() {
                    @Override
                    public void onSuccess(Object object) {
                        listener.onSuccess(object);
                    }

                    @Override
                    public void onError(int messageCode, String e) {
                        ToastUtils.showShort(e);
                    }
                });
    }



    public static void delArticle(Retrofit retrofit,String id,RetrofitListener listener) {
        retrofit
                .url(ApiService.DEL_ARTICLE)
                .addParam("userid",MyApp.userid)
                .addParam("id",id)
                .post(listener);
    }


}

